package com.example.j20240313springmvc.controller;

import com.example.j20240313springmvc.model.Student;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
@RestController
public class RequestController {
    @RequestMapping("/say1")
    public String say1() {
        return "hello Spring MVC";
    }
    @RequestMapping("/say2")
    public String say2() {
        return "你好 Spring MVC";
    }
    //接收一个参数
    @RequestMapping("/request1")
    public String request1(String name) {
        return "接收到参数：" + name;
    }
    @RequestMapping("/request2")
    public String request2(int age) {
        return "接受到参数：" + age;
    }
    @RequestMapping("/request3")
    public String request3(Integer age) {
        return "接受到参数：" + age;
    }
    //接收多个参数
    @RequestMapping("/request4")
    public String request4(String name, Integer age) {
        return "接收到参数,name: " + name + ",age:" + age;
    }
    //接收对象
    @RequestMapping("/request5")
    public String request5(Student s) {
        return "接收到参数,name: " + s;
    }
    //重命名参数
    @RequestMapping("/request6")
    public String request6(@RequestParam("name")String peopleName) {
        return "peopleName:" + peopleName;
    }
    @RequestMapping("/request7")
    public String request7(@RequestParam(value = "name", required = false)String peopleName) {
        return "peopleName:" + peopleName;
    }
    //接收数组
    @RequestMapping("/request8")
    public String request8(int[] array) {
        return "接收到参数：" + Arrays.toString(array);
    }
    //接收集合
    @RequestMapping("/request9")
    public String request9(@RequestParam List<Integer> list) {
        return "接收到参数：" + list;
    }
    //接收JSON
    @RequestMapping("/request10")
    public String request10(@RequestBody Student s) {
        return "接收到参数: " + s;
    }
    //获取URL中的参数
    @RequestMapping("/request11/{id}")
    public String request11(@PathVariable("id") Integer id) {
        return "接收到参数：" + id;
    }
    //接收文件
    @RequestMapping("/request12")
    public String request12(@RequestPart("file") MultipartFile file) {
        return "接收到文件：" + file.getOriginalFilename();
    }
    //获取Cookie
    @RequestMapping("/request13")
    public String getCookie1(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if(cookies != null) {
            Arrays.stream(cookies).forEach(x -> System.out.println(x.getName() + ":" + x.getValue()));
        }
        return "接收到Cookie";
    }
    @RequestMapping("/request14")
    public String getCookie2(@CookieValue("ck") String ck) {
        return "接收到Cookie,ck:" + ck;
    }

    //设置Session
    @RequestMapping("/request15")
    public String setSession1(HttpServletRequest request) {
        //从Cookie中获取SessionId再通过SessionId找到Session
        HttpSession session = request.getSession();
        session.setAttribute("name", "小明");
        return "设置Session成功";
    }
    @RequestMapping("/request16")
    public String setSession2(HttpSession session) {
        session.setAttribute("name", "小明");
        return "设置Session成功";
    }
    //获取Session
    @RequestMapping("/request17")
    public String getSession1(HttpServletRequest request) {
        //从Cookie中获取SessionId再通过SessionId找到Session
        HttpSession session = request.getSession();
        String name = (String)session.getAttribute("name");
        return "获取到Session:" + name;
    }
    @RequestMapping("/request18")
    public String getSession2(@SessionAttribute("name") String name) {
        return "获取到Session:" + name;
    }
    @RequestMapping("/request19")
    public String getHeader1(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        return "获取到，User-Agent：" + userAgent;
    }
    @RequestMapping("/request20")
    public String getHeader1(@RequestHeader("User-Agent")String userAgent) {
        return "获取到，User-Agent：" + userAgent;
    }
}
